#include "touchpixmapitem.h"
#include <QEvent>
#include "toolkit.h"
#include "paletteview.h"
TouchPixmapItem::TouchPixmapItem():
        m_bLock(false),
        m_scale(1.0),
        m_angle(0.0),
        m_type(0)
{
    setAcceptTouchEvents(true);
}

TouchPixmapItem::~TouchPixmapItem()
{

}

bool TouchPixmapItem::sceneEvent(QEvent *event)
{
    if(!m_bLock)
    {
        switch(event->type())
        {
        case QEvent::TouchBegin:
        case QEvent::TouchUpdate:
            {
                QTouchEvent *touchEvent = static_cast<QTouchEvent *>(event);
                m_touchPoints = touchEvent->touchPoints();
                if(m_touchPoints.count() == 2)
                {
                    ToolKit::m_paletteview->m_bTouch = true;
                    const QTouchEvent::TouchPoint &touchPoint0 = m_touchPoints.first();
                    const QTouchEvent::TouchPoint &touchPoint1 = m_touchPoints.last();
                    if(QLineF(touchPoint0.startPos(), touchPoint1.startPos()).length() > 0)
                    {
                        qreal currScale =
                                QLineF(touchPoint0.pos(), touchPoint1.pos()).length()
                                / QLineF(touchPoint0.startPos(), touchPoint1.startPos()).length();

                        setTransformOriginPoint(boundingRect().width() / 2,boundingRect().height() / 2);

                        if(currScale >= 1)
                            m_scale *= 1.1;
                        else
                            m_scale *= 0.9;

                        if(m_scale <= 2 && m_scale >= 0.7)
                            setScale(m_scale);
                        else if(m_scale > 2)
                            m_scale = 2;
                        else if(m_scale < 0.7)
                            m_scale = 0.7;
                    }
                }
            }
            break;
        case QEvent::TouchEnd:
            {
                ToolKit::m_paletteview->m_bTouch = false;
            }
            break;
        default:
            break;
        }
    }
    return true;
}
